//********************************新增页面
//验证当前登录名是否存在
 function checkLoginName(){
	 if(isNull($("#loginName").val())){
		 var url=BASE_PATH+"/admin/adPersonController/validaName?loginName="+loginName;
		 $.post(url, function(r) {
			var result = eval('('+ r +')'); 
			if(result.success){
				$.messager.alert("系统提示","登录名已存在","error");
				}
			});
		 }
	 }
 
 	//保存
function savePerson(){
	$.plugin.plugLoading("正在保存，请稍后...");
	if(isNull($("#name").val()) || isNull($("#loginName").val())|| isNull($("#loginPassword").val())){
		$.messager.alert("系统提示","保存失败！带*项是不允许为空,请填写","info");
        $.plugin.close();
		return;
	}
	 var loginName = $("#loginName").val();
	 var url=BASE_PATH+"/admin/adPersonController/validaName?loginName="+loginName;
	 $.post(url, function(r) {
		var result = eval('('+ r +')'); 
		if(result.success){
			$.plugin.close();
			$.messager.alert("系统提示","登录名已存在","error");
		}else{
			$.plugin.close();
			$.plugin.plugLoading("正在保存，请稍后...");
			 var url=BASE_PATH+"/admin/adPersonController/save";
			 var formParams = OasisHelper.form2object('ff');
		     $.ajax({
				type : "POST",
				url: url,
				async:false, 
				data: formParams,
				success: function(result) {
					$.plugin.close();
					//var result = eval('('+ r +')'); 
	    			if(result.success){
	    				closeWindow('新增用户');
	    				window.parent.messagerAlert('保存成功!');
	    			}else{
	    				$.messager.alert("系统提示","保存失败！","error");
	    			}
				}
			 });
		}
	 });
}

//**********************************修改页面
//验证当前登录名是否存在
function yanzhengName(){
	 var loginName = $("#loginName").val();
	 if(isNull($("#loginName").val())){
		 var personId = $("#personId").val();
		 var url=BASE_PATH+"/admin/adPersonController/validaName?personId="+personId+"&loginName="+loginName;
		 $.post(url, function(r) {
			var result = eval('('+ r +')'); 
			if(result.success){
				$.messager.alert("系统提示","登录名已存在","error");
			}
		});
	 }
}
//保存
function updateMember(){
	if(isNull($("#name").val()) || isNull($("#loginName").val())|| isNull($("#loginPassword").val())){
		$.messager.alert("系统提示","保存失败！带*项是不允许为空,请填写","info");
       $.plugin.close();
		return;
	}
	$.plugin.plugLoading("正在保存，请稍后...");
	 var url=BASE_PATH+"/admin/adPersonController/update";
    
    var formParams = OasisHelper.form2object('ff');
    $.ajax({
		type : "POST",
		url: url,
		async:false, 
		data: formParams,
		success: function(result) {
			$.plugin.close();
			//var result = eval('('+ r +')'); 
			if(result.success){
				closeWindow('修改用户');
				window.parent.messagerAlert('修改成功!');
			}else{
				$.messager.alert("系统提示",result.msg,"error");
			}
		}
	 });
}

//**********************************修改密码页面
/**
 * 修改密码
 * falge  page:用户管理列表页的修改密码按钮;  tab:左侧菜单栏用户管理中的修改密码页面
 */
function savePassword(falge){
	var personId = $("#personId").val();
	if(checkPassword(falge)){
		$.ajax({
			type: 'POST',
			url: BASE_PATH + '/admin/adPersonController/updatePerson',
			data: {'loginPassword' : $('#newPasswordToo').val(),'personId':personId},
			async: false,
			success: function(data){
				if(data && data.success){
					if('page' == falge){
						closeWindow('修改用户密码')
						window.parent.messagerAlert('修改成功!');
					}else if('tab' == falge){
						$.messager.show({
				            title: '提示',
				            timeout: 2000,
				            msg: '修改成功!'
				        });
						window.location.href = BASE_PATH + '/admin/adPersonController/toUpdateLoginPassword/'+personId;
					}
				}else{
					if('page' == falge){
						window.parent.getMessageAlert('修改失败!');
					}else if('tab' == falge){
						$.messager.show({
				            title: '提示',
				            timeout: 2000,
				            msg: '修改失败!'
				        });
					}
				}
			}
		});
	}
}
function checkPassword(falge){
	var numberTest = /^[0-9]+$/;
	var sameTest = /^(?:([0-9])\1{5})$/;
	 
	//校验原始密码
	if($('#resousePassword').val() != ''){
		if(!getCheckResousePassword($('#resousePassword').val())){
			if('page' == falge){
				window.parent.getMessageAlert("原始密码不正确!");
			}else if('tab' == falge){
				$("#loginPasswordTab").html("<span style='color:red;'>原始密码不正确!</span>");
			}
			return false;
		}else{
			if('tab' == falge){
				$("#loginPasswordTab").html("");
			}
		}
	}else{
		if('page' == falge){
			window.parent.getMessageAlert("原始密码不能为空!");
		}else if('tab' == falge){
			$("#loginPasswordTab").html("<span style='color:red;'>原始密码不能为空!</span>");
		}
		return false;
	}
	//校验新密码
	var newPassword = $('#newPassword').val();
	if(newPassword == '' ){
		if('page' == falge){
			window.parent.getMessageAlert("新密码不能为空!");
		}else if('tab' == falge){
			$("#newPasswordTab").html("<span style='color:red;'>新密码不能为空!</span>");
		}
		return false;
	}else{
		if('tab' == falge){
			$("#newPasswordTab").html("");
		}
	}
	//校验确认密码
	var newPasswordToo = $('#newPasswordToo').val();
	if(newPasswordToo != '' && newPasswordToo != newPassword){
		if('page' == falge){
			window.parent.getMessageAlert("确认密码与新密码不相符!");
		}else if('tab' == falge){
			$("#newPasswordTooTab").html("<span style='color:red;'>确认密码与新密码不相符!</span>");
		}
		return false;
	}else if(newPasswordToo == ''){
		if('page' == falge){
			window.parent.getMessageAlert("确认密码不能为空!");
		}else if('tab' == falge){
			$("#newPasswordTooTab").html("<span style='color:red;'>确认密码不能为空!</span>");
		}
		return false;
	}else{
		if('tab' == falge){
			$("#newPasswordTooTab").html("");
		}
	}
	
	return true;
}
//校验原始密码
function getCheckResousePassword(resousePassword){
	var flag = false;
	var personId = $("#personId").val();
	$.ajax({
		type: 'POST',
		url: BASE_PATH + "/admin/adPersonController/validaPassword",
		data: {'loginPassword' : resousePassword,'personId':personId},
		async: false,
		success: function(data){
			var result = eval('('+ data +')'); 
			if(result.success){
				flag = true;
			}
		}
	});
	return flag;
}

